<?php
namespace app\index\controller;

use think\Controller;
use app\admin\model\NewsModel;
use app\admin\model\ClassModel;
use app\admin\model\MemberModel;
use app\admin\model\RecordModel;
use app\admin\model\CommentModel;
use app\admin\model\MessageModel;

/**
 * 会员模块
 * @author Alinejun666
 * Date 2018-8-30
 */
class User extends controller
{
    public $userinfo;

    public function initialize()
    {
        // 验证登录是否过期
        $user = session('member');
        if (!empty($user)) {
            $diff = (int)time() - (int)strtotime($user['active_time']);
            if ($diff > 1800) {
                session("member", null);

                $this->redirect("index/login/index");
            }else{

                $this->userinfo = $user;
                session("member.active_time", date("Y-m-d H:i:s",time()));
            }
        }else{
            $this->redirect("index/login/index");
        }
    }
    // 个人中心
    public function wap()
    {
        if (!is_mobile_request()) {

            $this->redirect("index/user/index");
        }
        return $this->fetch('wap');
    }

    // 个人中心
    public function index()
    {
        $member = new MemberModel();

        $user = session('member');
        $info = $member::get($user['user_id']);
        unset($info['status']);
        unset($info['password']);

        $data = [
            'info'   => $info,
        ];

		return $this->fetch('index', $data);
    }

    // 修改信息
    public function edit()
    {
        $member = new MemberModel();

        // 提交数据处理
        if (request()->isAjax()) {

            // 获取数据
            $data = input("param.");

            if (empty($data['nickname'])) {
                echo json_encode(msg_error("昵称不能为空"));
                exit;
            }

            if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['nickname'])){
                echo json_encode(msg_error("昵称包含非法字符！"));
                exit;
            }

            if (!empty($data['password'])) {
                if(preg_match("/^\W+$/",$data['password'])){
                    echo json_encode(msg_error("密码为字母数组或下划线组成"));
                    exit;
                }

                $password = md5($data['password']."sanw666");
            }

            if (empty($data['headpic'])) {
                $headpic = "./public/static/index/headpic/temp".rand(1,10).".jpg";
            }else{
                $headpic = $data['headpic'];
            }

            $param = [
                'user_id'     => $data['user_id'],
                'headpic'     => $headpic,
                'nickname'    => $data['nickname'],
                'sex'         => $data['sex'],
                'job'         => $data['job'],
                'interest'    => $data['interest'],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];

            if (!empty($password)) {
                $param['password'] = $password;
            }

            // 修改
            $res  = $member->save_data($param);
            if (isset($res['msg'])) {
                echo json_encode($res);
                exit;
            }

            echo json_encode(msg_success("修改成功"));
            exit;
        }else{
            $this->redirect("index/user/index");
        }
    }

    // 浏览记录
    public function record()
    {
        $record  = new RecordModel();
        $news    = new NewsModel();
        
        $user  = $this->userinfo;
        $where = ['user_id' => $user['user_id']];
        $order = "create_time desc";
        $field = "*";
        $size  = 10;

        $data = $record->get_news_list($where, $order, $field, $size);

        $list = $data['list']['data'];
        foreach ($list as $k => $v) {
            $info = $news->get_info(['news_id' => $v['news_id']],"title");
            if (empty($info)) {
                unset($list[$k]);
            }else{
                $list[$k]['title'] = $info['title'];
            }
        }
        $res = [
            'data' => $list,
            'page' => $data['page'],
            'count'=> $data['count'],
        ];  

        return $this->fetch('record', $res);
    }

    // 我的评论
    public function comment()
    {
        $comment = new CommentModel();
        
        $user  = $this->userinfo;
        $where = ['user_id' => $user['user_id']];
        $order = "create_time desc";
        $field = "*";
        $size  = 10;

        // 分页查询
        $data = $comment->get_news_list($where, $order, $field, $size);

        $list = $data['list']['data'];
        $res = [
            'data' => $list,
            'page' => $data['page'],
            'count'=> $data['count'],
        ];  

        return $this->fetch('comment', $res);
    }

    // 我的留言
    public function message()
    {
        $message = new MessageModel();
        
        $user  = $this->userinfo;
        $where = [
            'type' => 1,
            'user_id' => $user['user_id'],
        ];
        $order = "create_time desc";
        $field = "*";
        $size  = 10;

        // 分页查询
        $data = $message->get_news_list($where, $order, $field, $size);

        $list = $data['list']['data'];
        foreach ($list as $k => $v) {
            $where = [
                'type' => 2,
                'reply_id' => $v['message_id'],
            ];
            $order = "create_time asc";
            $reply = $message->get_list($where, $order);
            if (!empty($reply)) {
                $list[$k]['reply'] = $reply;
            }
        }
        $res = [
            'data' => $list,
            'page' => $data['page'],
            'count'=> $data['count'],
        ];  

        return $this->fetch('message', $res);
    }
    // 退出
    public function loginout()
    {
        session('member', null);
        $this->redirect("index/index/index");
    }
}
